package com.timeshare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.timeshare.dto.SkillVO;
import com.timeshare.entity.Favorite;
import com.timeshare.entity.Skill;
import com.timeshare.entity.User;
import com.timeshare.mapper.FavoriteMapper;
import com.timeshare.mapper.SkillMapper;
import com.timeshare.mapper.UserMapper;
import com.timeshare.service.RecommendService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 推荐服务实现
 */
@Service
public class RecommendServiceImpl implements RecommendService {
    
    private final SkillMapper skillMapper;
    private final UserMapper userMapper;
    private final FavoriteMapper favoriteMapper;
    
    public RecommendServiceImpl(SkillMapper skillMapper, UserMapper userMapper, FavoriteMapper favoriteMapper) {
        this.skillMapper = skillMapper;
        this.userMapper = userMapper;
        this.favoriteMapper = favoriteMapper;
    }
    
    @Override
    public List<SkillVO> getRecommendedSkills(Long userId, Integer limit) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return getHotSkills(limit);
        }
        
        // 获取用户技能标签
        String userSkillTags = user.getSkillTags();
        if (userSkillTags == null || userSkillTags.isEmpty()) {
            return getHotSkills(limit);
        }
        
        // 获取用户收藏的技能分类
        LambdaQueryWrapper<Favorite> favoriteWrapper = new LambdaQueryWrapper<>();
        favoriteWrapper.eq(Favorite::getUserId, userId);
        List<Favorite> favorites = favoriteMapper.selectList(favoriteWrapper);
        
        Set<Integer> favoriteCategories = new HashSet<>();
        for (Favorite favorite : favorites) {
            Skill skill = skillMapper.selectById(favorite.getSkillId());
            if (skill != null) {
                favoriteCategories.add(skill.getCategory());
            }
        }
        
        // 查询推荐技能
        LambdaQueryWrapper<Skill> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Skill::getStatus, 1) // 只查询已发布的
               .ne(Skill::getUserId, userId); // 排除自己发布的
        
        // 优先推荐同分类的技能
        if (!favoriteCategories.isEmpty()) {
            wrapper.in(Skill::getCategory, favoriteCategories);
        }
        
        wrapper.orderByDesc(Skill::getViewCount)
               .orderByDesc(Skill::getFavoriteCount)
               .last("LIMIT " + limit);
        
        List<Skill> skills = skillMapper.selectList(wrapper);
        
        // 转换为VO
        return skills.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    
    @Override
    public List<SkillVO> getHotSkills(Integer limit) {
        LambdaQueryWrapper<Skill> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Skill::getStatus, 1)
               .orderByDesc(Skill::getViewCount)
               .orderByDesc(Skill::getFavoriteCount)
               .orderByDesc(Skill::getAppointmentCount)
               .last("LIMIT " + limit);
        
        List<Skill> skills = skillMapper.selectList(wrapper);
        
        return skills.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 转换为VO
     */
    private SkillVO convertToVO(Skill skill) {
        SkillVO vo = new SkillVO();
        BeanUtil.copyProperties(skill, vo);
        
        User user = userMapper.selectById(skill.getUserId());
        if (user != null) {
            vo.setUsername(user.getUsername());
            vo.setUserNickname(user.getNickname());
            vo.setUserAvatar(user.getAvatar());
        }
        
        return vo;
    }
}

